package com.technology.education.ui.student.study_record

import androidx.core.os.bundleOf
import com.angcyo.dsladapter.removeItem
import com.angcyo.dsladapter.toDslItemList
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.imageview.ShapeableImageView
import com.technology.education.R
import com.technology.education.base.BaseRecyclerTopBarFragment
import com.technology.education.data.model.TeachOrderModel
import com.technology.education.data.model.TeacherOrderList
import com.technology.education.ext.loadImage
import com.technology.education.ext.navigation
import com.technology.education.ext.setPageItems
import me.hgj.jetpackmvvm.ext.parseState

class StudyRecordFragment : BaseRecyclerTopBarFragment<StudyRecordViewModel>() {
    override fun initViews() {
        setTitle("辅导记录")
        initLoadMore()
    }

    override fun lazyLoadData() {
        loading()
    }

    private fun setUI(list: List<TeachOrderModel>) {
        renderAdapter {
            setPageItems(list.toDslItemList(R.layout.item_study_record) {
                itemBindOverride = { itemHolder, _, _, _ ->
                    itemData?.let {
                        if (it is TeachOrderModel) {
                            itemTag = it.id
                            itemHolder.tv(R.id.tv_name)?.text =
                                "辅导老师: ${it.teacher?.name ?: "暂无老师接单"}"
                            itemHolder.tv(R.id.tv_info)?.text = "问题描述: ${it.question}"
                            itemHolder.tv(R.id.tv_date)?.text = "时间: ${it.placeTime}"
                            itemHolder.v<ShapeableImageView>(R.id.iv_image)?.apply {
                                loadImage(it.questionImage, this)
                            }
                            itemHolder.clickItem { view ->
                                navigation(
                                    view, R.id.action_studyRecordFragment_to_orderDetailFragment,
                                    bundleOf("id" to it.id)
                                )
                            }
                            itemHolder.longClickItem { _ ->
                                delDialog(it.id)
                            }
                        }
                    }

                }
            }, mViewModel.pageIndex)
        }
    }

    private fun delDialog(id: String) {
        MaterialAlertDialogBuilder(requireContext()).setTitle("提示")
            .setMessage("是否删除该订单?")
            .setNegativeButton(
                "确定"
            ) { p0, _ ->
                p0?.dismiss()
                mViewModel.delOrder(id)
            }
            .setPositiveButton("取消") { p0, _ ->
                p0?.dismiss()
            }.create()
            .show()
    }

    override fun createObserver() {
        mViewModel.result.observe(this) {
            parseState(it, onSuccess = { data ->
                hasMore(data.total)
                loadFinish()
                setUI(data.list)
            }, onError = {
                if (mViewModel.pageIndex == 1) {
                    loadError()
                }
            })
        }
        mViewModel.delResult.observe(this) {
            renderAdapter {
                removeItem(it)
            }
        }
    }
}